package khmt.lvtn.dhbk.summary;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class RetrieveOntology {
	static RetrieveOntology instant = new RetrieveOntology(); 

	public RetrieveOntology() {
		super();
		// TODO Auto-generated constructor stub
		
	}
	public static RetrieveOntology getInstant(){
		return instant;
	}
	public void readOntology(String thucthe, String thuoctinh, String tinhtu,
			String dongtu, String trotu, String phudinh, String daitu) {

		// /////////////////////////////////////////
		try {

			// parser file owl
			File owlFile = new File("ontology/Ontology.owl");
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(owlFile);
			doc.getDocumentElement().normalize();
			NodeList entityList = doc.getElementsByTagName("ClassAssertion");
			NodeList infoList = doc.getElementsByTagName("SubClassOf");
			NodeList dataProp = doc
					.getElementsByTagName("DataPropertyAssertion");

			// number node
			System.out.println(entityList.getLength());

			// List dien thoai di dong
			List<String> dtdd = new ArrayList<String>();

			// List may tinh xach tay
			List<String> mtxt = new ArrayList<String>();

			// file thucthe.txt
			File fileThucThe = new File(thucthe);
			FileWriter wrFileEntity = new FileWriter(fileThucThe);
			BufferedWriter buffWriteEntity = new BufferedWriter(wrFileEntity);

			// file daitu.txt
			File fileDaiTu = new File(daitu);
			FileWriter wrFilePronoun = new FileWriter(fileDaiTu);
			BufferedWriter buffWritePronoun = new BufferedWriter(wrFilePronoun);

			// file phudinh.txt
			File filePhuDinh = new File(phudinh);
			FileWriter wrFileNegative = new FileWriter(filePhuDinh);
			BufferedWriter buffWriteNegative = new BufferedWriter(
					wrFileNegative);

			// file tinhtu.txt
			File fileTinhTu = new File(tinhtu);
			FileWriter wrFileAdj = new FileWriter(fileTinhTu);
			BufferedWriter buffWriteAdj = new BufferedWriter(wrFileAdj);

			// file dongtu.txt
			File fileDongTu = new File(dongtu);
			FileWriter wrFileVerb = new FileWriter(fileDongTu);
			BufferedWriter buffWriteVerb = new BufferedWriter(wrFileVerb);

			// file trotu.txt
			File fileTroTu = new File(trotu);
			FileWriter wrFileAux = new FileWriter(fileTroTu);
			BufferedWriter buffWriteAux = new BufferedWriter(wrFileAux);

			// parse file Ontology lấy thông tin
			for (int i = 0; i < entityList.getLength(); i++) {
				Node nNode = entityList.item(i);
				Element eElement = (Element) nNode;
				Element entity = (Element) eElement.getElementsByTagName(
						"NamedIndividual").item(0);
				Element classEntity = (Element) eElement.getElementsByTagName(
						"Class").item(0);
				String st = entity.getAttribute("IRI").replaceFirst("#", "")
						.replaceAll("_", " ");
				String st1 = classEntity.getAttribute("IRI")
						.replaceFirst("#", "").replaceAll("_", " ");

				if (st1.equals("Điện thoại di động")) {
					dtdd.add(st);
					
					buffWriteEntity.write(st);
					buffWriteEntity.newLine();
				} else if (st1.equals("Máy tính xách tay")) {
					mtxt.add(st);
					
					buffWriteEntity.write(st);
					buffWriteEntity.newLine();
				} else if (st1.equals("Đại từ")) {
					
					buffWritePronoun.write(st);
					buffWritePronoun.newLine();
				} else if (st1.equals("Phủ định")) {
					
					buffWriteNegative.write(st);
					buffWriteNegative.newLine();
				} else if (st1.equals("Tính từ")) {
					for (int j = 0; j < dataProp.getLength(); j++) {
						Node dataNode = dataProp.item(j);
						Element eData = (Element) dataNode;
						Element nameIndi = (Element) eData
								.getElementsByTagName("NamedIndividual")
								.item(0);
						Element eLiteral = (Element) eData
								.getElementsByTagName("Literal").item(0);
						String compareSt = nameIndi.getAttribute("IRI")
								.replaceFirst("#", "").replaceAll("_", " ");
						String num = eLiteral.getTextContent();
						if (st.equals(compareSt)) {
							String writeAdj = "<adj>" + st + "<p>" + num
									+ "<e>";
							buffWriteAdj.write(writeAdj);
							buffWriteAdj.newLine();
						}
					}

				} else if (st1.equals("Động từ")) {
					for (int j = 0; j < dataProp.getLength(); j++) {
						Node dataNode = dataProp.item(j);
						Element eData = (Element) dataNode;
						Element nameIndi = (Element) eData
								.getElementsByTagName("NamedIndividual")
								.item(0);
						Element eLiteral = (Element) eData
								.getElementsByTagName("Literal").item(0);
						String compareSt = nameIndi.getAttribute("IRI")
								.replaceFirst("#", "").replaceAll("_", " ");
						String num = eLiteral.getTextContent();
						if (st.equals(compareSt)) {
							String writeVerb = "<verb>" + st + "<p>" + num
									+ "<e>";
							buffWriteVerb.write(writeVerb);
							buffWriteVerb.newLine();
						}
					}
				} else if (st1.equals("Trợ từ")) {
					for (int j = 0; j < dataProp.getLength(); j++) {
						Node dataNode = dataProp.item(j);
						Element eData = (Element) dataNode;
						Element nameIndi = (Element) eData
								.getElementsByTagName("NamedIndividual")
								.item(0);
						Element eLiteral = (Element) eData
								.getElementsByTagName("Literal").item(0);
						String compareSt = nameIndi.getAttribute("IRI")
								.replaceFirst("#", "").replaceAll("_", " ");
						String num = eLiteral.getTextContent();
						if (st.equals(compareSt)) {
							String writeAux = "<aux>" + st + "<p>" + num
									+ "<e>";
							buffWriteAux.write(writeAux);
							buffWriteAux.newLine();
						}
					}
				}
			}
			buffWriteNegative.close();
			wrFileNegative.close();
			buffWriteAux.close();
			wrFileAux.close();
			buffWritePronoun.close();
			wrFilePronoun.close();
			buffWriteVerb.close();
			wrFileVerb.close();
			buffWriteAdj.close();
			wrFileAdj.close();
			buffWriteEntity.close();
			wrFileEntity.close();

			// file thuoctinh.txt
			File fileThuocTinh = new File(thuoctinh);
			FileWriter wrFileProp = new FileWriter(fileThuocTinh);
			BufferedWriter buffWriteProp = new BufferedWriter(wrFileProp);

			// Lap lay thuoc tinh
			for (int i = 0; i < infoList.getLength(); i++) {
				Node nNode = infoList.item(i);
				Element eElement = (Element) nNode;
				Element child = (Element) eElement
						.getElementsByTagName("Class").item(0);
				Element parent = (Element) eElement.getElementsByTagName(
						"Class").item(1);
				String st = child.getAttribute("IRI").replaceFirst("#", "")
						.replaceAll("_", " ");
				String st1 = parent.getAttribute("IRI").replaceFirst("#", "")
						.replaceAll("_", " ");

				if (st1.equals("Thông tin sản phẩm")) {
					
					buffWriteProp.write(st);
					buffWriteProp.newLine();
				}
			}
			buffWriteProp.close();
			wrFileProp.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	// /////////////////////////////////////////

}
